﻿#region Using Directives

using EnvDTE;
using System;
using System.Text;
using EnvDTE80;
using System.IO;
using System.ComponentModel;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;


#endregion

namespace FeatureBuilder.ContribLib.ValueProviders
{
    /// <summary>
    ///Returns the selected Solution Folder on the Solution Explorer
    /// </summary>
    [DisplayName("CurrentSelectedSolutionProvider")]
    [Description("Returns the selected Solution Folder on the Solution Explorer")]
    [Category("IDE")]
    public class SelectedSolutionFolderProvider : ValueProvider
    {
        /// <summary>
        /// Gets the ServiceProvider that we will use to get a reference to DTE.
        /// </summary>
        [Import(typeof(SVsServiceProvider))]
        public IServiceProvider ServiceProvider { get; private set; }

        #region Overrides

        /// <summary>
        /// Uses the DTE.SelectedItems collection to get the current selected project
        /// </summary>
        public override object Evaluate()
        {
            object folder = null;
            DTE solutionDTE = this.ServiceProvider.GetService(typeof(DTE)) as DTE;
            if (solutionDTE != null)
            {
                try
                {
                    Project project = solutionDTE.SelectedItems.Item(1).Project as Project;
                    if (project != null)
                    {
                        SolutionFolder solutionFolder = project.Object as SolutionFolder;
                        folder = solutionFolder;
                    }
                }
                catch
                {
                    folder = null;
                }
            }
            return folder;
        }

        #endregion
    }
}

